package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.studyadmin.dto.ArticleInsertBO;
import com.studyadmin.entity.Category;
import com.studyadmin.entity.PersonArticle;
import com.studyadmin.entity.Student;
import com.studyadmin.mapper.CategoryMapper;
import com.studyadmin.mapper.PersonArticleMapper;
import com.studyadmin.mapper.StudentMapper;
import com.studyadmin.service.PersonArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.EncryptUtils;
import com.studyadmin.util.JsonUtil;
import com.studyadmin.util.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * <p>
 * 文章管理表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-27
 */
@Service
public class PersonArticleServiceImpl extends ServiceImpl<PersonArticleMapper, PersonArticle> implements PersonArticleService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired(required = false)
    PersonArticleMapper articleMapper;
    @Autowired(required = false)
    StudentMapper studentMapper;

    /**
     * 文章的分类查询
     * @param categoryName 分类名
     * @return 查询结果
     */
    @Override
    public JsonUtil getCategoriesByName(String categoryName) {
        JsonUtil jsonUtil = new JsonUtil();
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.like("category_name",categoryName);


        List<Category> categoriesByName = categoryMapper.selectList(categoryQueryWrapper);
        if(categoriesByName.size()<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("查询失败");
        }else {
            jsonUtil.setCode(0);
            jsonUtil.setMsg("查询成功");
            jsonUtil.setData(categoriesByName);
        }

        return jsonUtil;
    }

    /**
     * 文章的插入
     * @param bo
     * @return
     */
    @Override
    public JsonUtil insertOrUpdateArticle(ArticleInsertBO bo) {
        //根据分类名找到对于Id
        JsonUtil jsonUtil = new JsonUtil();
        Category category=categoryMapper.selectOne(
                new QueryWrapper<Category>()
                        .select("category_id")
                        .eq("category_name",bo.getCategoryName())
        );
        if(category==null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
        }
//        String author=
        Student student=studentMapper.selectOne(
                new QueryWrapper<Student>()
                        .select("stu_name")
                        .eq("stu_num",bo.getUserId())
        );
        String author=student.getStuName();
        PersonArticle personArticle = new PersonArticle();
        personArticle.setAuthor(author);//作者

        //检查敏感字，提示替换。
        //标题，检测敏感词
        String title= SensitiveWordsUtil.textFiltering(bo.getTitle());
//        System.out.println(s);
        if(!title.equals("")){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("发表失败,请标题中删除以下敏感词："+title);
        return jsonUtil;
        }


//        System.out.println(SensitiveWordHelper.contains(bo.getTitle()));
//        if(SensitiveWordHelper.contains(bo.getTitle())){
//            jsonUtil.setCode(1);
//            String listString = "";
//            for (String s : SensitiveWordHelper.findAll(bo.getTitle())) {
//                listString += s + "\t";
//            }
//            System.out.println("zifu :"+listString);
//            jsonUtil.setMsg("发表失败,请标题中删除以下敏感词："+listString);
//        return jsonUtil;
//        }textFiltering
        personArticle.setTitle(bo.getTitle());//标题
        personArticle.setUserId(bo.getUserId());
        personArticle.setCategoryId(category.getCategoryId());
        //方法一：官方过滤
//        if(SensitiveWordHelper.contains(bo.getContent())){
//            jsonUtil.setCode(1);
//            String listString = "";
//            for (String s : SensitiveWordHelper.findAll(bo.getContent())) {
//                listString += s + "\t";
//            }
//            System.out.println("zifu :"+listString);
//            jsonUtil.setMsg("发表失败,请文章中删除以下敏感词："+listString);
//            return jsonUtil;
//        }
        //方法二：自定义过滤
        //过滤笔记敏感词
        String content= SensitiveWordsUtil.textFiltering(bo.getTitle());
        if(!content.equals("")){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("发表失败,请笔记中删除以下敏感词："+content);
            return jsonUtil;
        }
        personArticle.setContent(bo.getContent());
        personArticle.setArtStatus(1);
        personArticle.setArtStatus(bo.getArtStatus());
        personArticle.setTotalWords(EncryptUtils.wordCount(bo.getContent()));
        int resoult=articleMapper.insert(personArticle);
        if(resoult>0){
            jsonUtil.setCode(0);
            jsonUtil.setMsg("发表成功");
        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("发表失败");
        }

        return jsonUtil;
    }

    @Override
    public JsonUtil articleDetailsByStuSer(String stuNum, String articleId) {
        JsonUtil jsonUtil = new JsonUtil();
        PersonArticle personArticle=articleMapper.selectOne(
                new QueryWrapper<PersonArticle>()
                        .eq("user_id",stuNum)
                        .eq("id",Integer.parseInt(articleId))
        );
        System.out.println("查询数据："+personArticle);
        jsonUtil.setCode(0);
        jsonUtil.setData(personArticle);

        return jsonUtil;
    }

    @Override
    public IPage pageForArt(IPage<PersonArticle> page, Wrapper wrapper) {
        return articleMapper.pageForArtMapper(page,wrapper);
    }

}
